package com.taxigang.client.uibinder;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.shared.AwardInfo;
import com.taxigang.shared.TaxiGanguserInfo;

public class Awards extends TaxiGangPanel {

	private static AwardsUiBinder uiBinder = GWT
	.create(AwardsUiBinder.class);

	interface AwardsUiBinder extends UiBinder<Widget, Awards> {
	}

	@UiField Panel contentPanel;
	@UiField Button closeButton;

	@UiField Label headerLabel;


	public Awards() {
		initWidget(uiBinder.createAndBindUi(this));
		bind();
	}

	private void bind(){
		closeButton.setText(TaxiGangCreator.taxiGangConstants.close());
		
		closeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO store nickname
				taxiGangRoute.show(PanelType.MYTAXIGANGS);
			}
		});


	}

	public void show(){
		super.show();
		showContent();
	}

	private void showContent(){
		TaxiGanguserInfo user = Session.getInstance().getCurrentShowAwardsUser();
		headerLabel.setText(user.getDisplayName());
		contentPanel.clear();
		ScrollPanel scrollPane = new ScrollPanel();
		scrollPane.setHeight("250px");
	    FlexTable layout = new FlexTable();
	    layout.setCellSpacing(6);
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    

	    // Add a title to the form
	    //layout.setHTML(0, 0, user.getDisplayName());
	    //cellFormatter.setColSpan(0, 0, 2);
	    cellFormatter.setHorizontalAlignment(
	        0, 0, HasHorizontalAlignment.ALIGN_CENTER);

		AwardInfo[] awards = user.getAwards();
		if (awards != null && awards.length != 0){
			int i = 0;
			for (AwardInfo awardInfo : user.getAwards()){
				//DecoratorPanel p = new DecoratorPanel();
				//HorizontalPanel hp = new HorizontalPanel();
				
				AggregateArticleRating rating = new AggregateArticleRating();
				rating.numVotes = user.getAwards().length;
				rating.rating = ""+awardInfo.getPoints();
				rating.userVoted = true;
				ArticleRatingWidget articleRatingWidget =  new ArticleRatingWidget("articleId", false, true, HasHorizontalAlignment.ALIGN_CENTER, rating,awardInfo.getPoints());
				
			    layout.setWidget(i, 0, articleRatingWidget);
			    Label awardTextLabel = new Label(awardInfo.getAwardText());
			    awardTextLabel.setStylePrimaryName("memberLabel");
			    layout.setWidget(i, 1, awardTextLabel);

				i++;
			}
			scrollPane.add(layout);
			contentPanel.add(scrollPane);
		} else {
			DecoratorPanel p = new DecoratorPanel();
			p.add(new Label("No awards yet"));
			contentPanel.add(p);
		}
	}

}
